package com.smt.modules.bus.service.impl;

import com.smt.common.enums.Code;
import com.smt.common.utils.*;
import com.smt.modules.bus.entity.BillEntity;
import com.smt.modules.bus.entity.GradeEntity;
import com.smt.modules.bus.entity.MemberUserEntity;
import com.smt.modules.bus.service.BillService;
import com.smt.modules.bus.service.GradeService;
import com.smt.modules.bus.service.MemberUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.Map;
import java.util.Objects;

import com.baomidou.mybatisplus.mapper.EntityWrapper;
import com.baomidou.mybatisplus.plugins.Page;
import com.baomidou.mybatisplus.service.impl.ServiceImpl;

import com.smt.modules.bus.dao.ConsumptionFileDao;
import com.smt.modules.bus.entity.ConsumptionFileEntity;
import com.smt.modules.bus.service.ConsumptionFileService;


@Service("consumptionFileService")
public class ConsumptionFileServiceImpl extends ServiceImpl<ConsumptionFileDao, ConsumptionFileEntity> implements ConsumptionFileService {


    @Autowired
    private BillUtils billUtils;

    @Autowired
    private GradeService gradeService;

    @Autowired
    private BillService billService;

    /**
     * 获取文件列表
     *
     * @return
     */
    @Override
    public R list() {
        List<ConsumptionFileEntity> consumptionFileEntities = selectList(new EntityWrapper<ConsumptionFileEntity>()
        .setSqlSelect("id","file_title","file_overview","file_integral","stage_id"));
        PageUtils pageUtils = new PageUtils(consumptionFileEntities,
                consumptionFileEntities.size(),10,1);
        return R.ok().put("fileList",pageUtils);
    }

    @Override
    public R info(Long id, MemberUserEntity memberUserEntity) {
        Long userId = memberUserEntity.getUserId();
        GradeEntity gradeEntity = gradeService.selectOne(new EntityWrapper<GradeEntity>()
                .eq("member_id", userId));
        ConsumptionFileEntity consumptionFileEntity = selectById(id);
        //文件id
        Long conId = consumptionFileEntity.getId();
        //文件积分
        Integer fileIntegral = consumptionFileEntity.getFileIntegral();
        //文件地址
        String fileUrl = consumptionFileEntity.getFileUrl();
        if(!Objects.isNull(gradeEntity)){
            Integer integral = gradeEntity.getIntegral();
            if(integral < fileIntegral){
                return R.error(EnumToKeyUtil.toInt(Code.QUERY_DATA_ERROR),"您的积分不够！");
            }else{
                BillEntity billEntity = billService.selectOne(new EntityWrapper<BillEntity>()
                        .eq("file_load_id", conId)
                        .eq("expenditure", 2)
                        .eq("bus_business_id", userId)
                        .eq("is_reward", 2));
                if(!Objects.isNull(billEntity)){
                    return R.ok("您已经下载过该文件").put("fileUrl",fileUrl);
                }
            }
        }
        //文件标题
        String fileTitle = consumptionFileEntity.getFileTitle();
        //消耗积分id
        Long consumptionId = consumptionFileEntity.getId();
        boolean b = billUtils.ConsumptionFile("文件"+fileTitle+"下载",fileIntegral,consumptionId, memberUserEntity);
        if(b){
            return R.ok().put("fileUrl",fileUrl);
        }else{
            return R.error("文件下载失败");
        }
    }

    @Override
    public R searchFileName(String name) {
        List<ConsumptionFileEntity> fileList = selectList(new EntityWrapper<ConsumptionFileEntity>()
                .like("file_title", name));
        PageUtils pageUtils = new PageUtils(fileList,fileList.size(),10,1);
        return R.ok().put("fileList",pageUtils);
    }


}
